package client.ui.component.edittext;

import client.assets.Icons;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
  * @Description 密码输入框
  * @author 孔明浩
  * @Date 2020/3/31
  */
public class PasswordEdit extends TextEdit {
    private char[] password;
    public PasswordEdit() {
    }

    public PasswordEdit(String hint) {
        super(hint);
    }

    @Override
    protected void initial() {
        this.textField = new JPasswordField();
        //重新设置密码框的回显
        ((JPasswordField)PasswordEdit.this.textField).setEchoChar('*');
        this.endImage = Icons.show_password;
        super.initial();
        this.endLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                ((JPasswordField)PasswordEdit.this.textField).setEchoChar((char)0);
                PasswordEdit.this.endLabel.setIcon(Icons.clicked_show_password);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                ((JPasswordField)PasswordEdit.this.textField).setEchoChar('*');
                PasswordEdit.this.endLabel.setForeground(null);
                PasswordEdit.this.endLabel.setIcon(Icons.show_password);
            }

        });
    }

    public char[] getPassword(){
        return ((JPasswordField)this.textField).getPassword();
    }
}
